var app;
		
		function get(id) {
			return document.getElementById(id);
		}

		function init() {
			app = chrome.extension.getBackgroundPage();
			
			var items = app.config.getSelectedLangs();

			initSelect(get("sourceSelect"), items, app.config.getSourceLang());			
			initSelect(get("targetSelect"), items, app.config.getTargetLang());

			var lastQuery = app.config.getLastQuery();

			var input = get("textInput");
			input.value = app.config.getLastQuery();

			input.select();
		}
		
		function initSelect(select, items, selected) {
			for (var i = 0; i < items.length; i++)
				addOption(select, items[i].text, items[i].value, i, selected);
		}
		
		function addOption(select, text, value, index, selectedVal) {
			var option = document.createElement("option");
			option.text = text;
			option.value = value;

			if (value == selectedVal)
				option.selected = "selected";

			select.add(option, select.options[index]);
		}

		function translate() {
			var text = get('textInput').value;
			var source = getSelectedValue('sourceSelect');
			var target = getSelectedValue('targetSelect');

			app.translate(text, source, target, displayResult, true);
		}

		function displayResult(result, text) {
			var res;

			if (!result)
				res = "no data";
			else
				res = app.renderTranslation(result);

			var output = get("translation");
			output.innerHTML = res;

			get("textInput").focus();
		}
		
		function getSelectedValue(selectId) {
			var select = get(selectId);
			return select.options[select.selectedIndex].value;
		}

		function setSelectedValue(selectId, value) {
			var select = get(selectId);
			for (var i = 0; i < select.options.length; i++)
				if (select.options[i].value == value) {
					select.options[i].selected = "selected";
				}
		}

		function swapLang() {
			
			var source = getSelectedValue('sourceSelect');
			var destination = getSelectedValue('targetSelect');

			setSelectedValue('sourceSelect', destination);
			setSelectedValue('targetSelect', source);
		}

		function onKeyDown() {
			if (event.keyCode == 69 && event.ctrlKey) {
				swapLang();
				return;
			}
		}

		function showOption()
		{
			var url = chrome.extension.getURL("options.html");

			window.open(url, "_blank");
		}

		function onTextFieldKeyPress() {
			if (event.keyCode == 13) {
				get("textInput").blur();

				translate();

				return;
			}
		}
		
		//need for autofocus
		/*if(location.search !== "?foo") {
			location.search = "?foo";
			throw new Error;  // load everything on the next page;
                    // stop execution on this page
		}*/
		
		document.addEventListener('DOMContentLoaded', init);
		
		document.addEventListener('keydown', onKeyDown);
		
		document.getElementById('swapButton').addEventListener('click', swapLang);
		document.getElementById('showOptionsButton').addEventListener('click', showOption);
		document.getElementById('translateButton').addEventListener('click', translate);
		
		document.getElementById('textInput').addEventListener('keypress', onTextFieldKeyPress);
		